var selectedRow = '';
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoBizEnumPage();
        },
    }
});

function doRefresh() {
    selectedRow = '';
    var enumName = $.trim($('#enumName').val());
    var status = $('#status').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'bizEnum/pageFindBizEnumData?t=' + new Date().getTime(),
        query: {
            enumName : enumName,
            status : status,
        },
    });
}

function getParams(params) {
    var pageNo = params.pageNumber;
    var pageSize = params.pageSize;
     var postData = {
         pageNo:pageNo,
         pageSize:pageSize
     };
    $("#searchForm").serializeArray().map((function (item,index,input) {
        postData[item.name] = item.value;
    }));
    return postData;
}
function getGroupParams(params) {
    var pageNo = params.pageNumber;
    var pageSize = params.pageSize;
     var postData = {
         pageNo:pageNo,
         pageSize:pageSize
     };
    return postData;
}

function openAddBizEnumPage() {
    if (!groupSelectRow || groupSelectRow == '') {
        toastr.error('请选择左侧一行后再进行操作');
        return;
    }
    openEditPage('add', 0);
}

function openEditBizEnumPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error('请选择一行后再进行编辑');
        return;
    }
    openEditPage('edit', selectedRow.id);
}

function openInfoBizEnumPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('info', selectedRow.id);
}

function openEditPage(command, id) {
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    var GrouptableOptions = $('#GroupdataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'bizEnum/editBizEnumPage?type=' + command + '&id=' + id + '&t=' + new Date().getTime() +
        '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize+
       '&GpageNo=' + GrouptableOptions.pageNumber + '&GpageSize=' + GrouptableOptions.pageSize;
}

function changeState() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var status = 'enabled';
    if (selectedRow.status == 'enabled') {
        status = 'disabled';
    }
    var postData = {
        id : selectedRow.id,
        status : status,
        labelState : selectedRow.labelState,
        labelImageUrl : selectedRow.labelImageUrl,
        labelInstruction : selectedRow.labelInstruction
    }
    $.ajax({
        type: 'post',
        url: basePath + 'bizEnum/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('修改成功');
                doRefresh();
            } else  if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('修改失败');
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

function doDeleteBizEnum() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error('请选择一行后再进行删除');
        return;
    }
    var htmlStr = '删除会导致数据库变动，确定要删除【' + selectedRow.enumName + '】吗？';
    swalToastr('', htmlStr, 'warning', deleteBizEnum);
}

function deleteBizEnum() {
    var postData = {
        id : selectedRow.id,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'bizEnum/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            } else  if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

/******************************右侧点击事件*********************************************/
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $(".success").removeClass("success");
    $(ele).addClass("success");
    changeStatueBtn(row.status == 'enabled',"冻结","恢复");
    selectedRow = row;
});


/*****************************左侧点击事件**********************************************/
var  groupSelectRow = null;
$("#GroupdataTable").on("click-row.bs.table",function(e,row,ele){
    $(".success").removeClass("success");
    $(ele).addClass("success");
    groupSelectRow = row;
    $("#groupCode").val(row.dictCode);
    doRefresh();
});


function formatStatus(value, row, index){
    var title;
    if (value == 'enabled') {
        title = "有效";
    } else {
        title = "无效";
    }
    return title;
}

function formatBizName(value, row, index){
    var flag = false,name;
    if(value != null || value != '' ){
        bizCodeTypeMap.map((function (item,index,input) {
            if(item.code == value){
                flag = true;
                name = item.name;
            }
        }));
    }
    return flag?name:value;
}


function onceCheck(value,row,$elem){
    if(hit ==1 && row.dictCode == groupCode ){
        index =$elem;
        setTimeout(function(){
            $("#dataAppTable > tbody > tr[data-index='" +index+"']").addClass("success");
        },500);
        refresh ++;
    }
    return value;
}


function formatTime(value,row,$elem) {
   return dateFtt("yyyy-MM-dd hh:mm",new Date(value));
}